package meconsea.life.cms.web;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import meconsea.life.admin.accessor.AdminUserAccessor;
import meconsea.life.admin.accessor.AdminUserLogAccessor;
import meconsea.life.admin.accessor.IndicateAccessor;
import meconsea.life.admin.entity.AdminUser;
import meconsea.life.admin.entity.AdminUserLog;
import meconsea.life.admin.entity.Indicate;
import meconsea.life.cms.accessor.SArticleAccessor;
import meconsea.life.cms.accessor.XArticleAccessor;
import meconsea.life.cms.accessor.YArticleAccessor;
import meconsea.life.cms.accessor.ZArticleAccessor;
import meconsea.life.cms.entity.Article;
import meconsea.life.util.Constant;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;

import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 该类是内容管理中文章管理的UI初始化Action。用于完成文章类别的tree。
 * @author Administrator
 *
 */
public class ArticleIndex extends ActionSupport {
	private final static Logger log = Logger.getLogger(ArticleIndex.class);
	
	private String rootCls;
	private String artType;
	private String rootClsInfo;
	public int getFirst() {
		return first;
	}

	public void setFirst(int first) {
		this.first = first;
	}

	private int first;
	public String getArtType() {
		return artType;
	}

	public void setArtType(String artType) {
		this.artType = artType;
	}

	public String getRootClsInfo() {
		return rootClsInfo;
	}

	public void setRootClsInfo(String rootClsInfo) {
		this.rootClsInfo = rootClsInfo;
	}

	private String action;
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	@Inject
	private AdminUserLogAccessor adminUserLogAccessor;
	
	@Inject
	private IndicateAccessor indicateAccessor;
	
	@Inject
	private YArticleAccessor yArticleAccessor;
	
	@Inject
	private SArticleAccessor sArticleAccessor;
	
	@Inject
	private ZArticleAccessor zArticleAccessor;
	
	@Inject
	private XArticleAccessor xArticleAccessor;
	
	
	public String execute(){
		Map session = ActionContext.getContext().getSession();
		AdminUser au = (AdminUser)session.get(meconsea.life.util.Constant.ADMIN_USER_SESSION);
		if(au == null){
			return Constant.ADMIN_LOGIN;
		}
		AdminUserLog aul = new AdminUserLog();
		aul.setUserid(au.getId());
		aul.setLoginfo("访问内容管理"+getRootCls()+";Action="+getAction());
		adminUserLogAccessor.save(aul);
		if("index".equals(getAction())){
			return "ArticleIndex";
		}else if("tree".equals(getAction())){//生成tree
			Indicate ind = indicateAccessor.getIndicateByClsCode(getRootCls());
			setRootClsInfo(ind.getClsName()+":"+ind.getClsCode());
			return "tree";
		}else if("content".equals(getAction())){
			Indicate ind = indicateAccessor.getIndicateByClsCode(getRootCls());
			setRootClsInfo(ind.getClsName()+":"+ind.getClsCode());
			return "content";
		}else if("list".equals(getAction())){//Ajax获取列表
			HttpServletResponse response = ServletActionContext.getResponse(); 
			HttpServletRequest request = ServletActionContext.getRequest();
			response.setContentType("text/xml");
			response.setHeader("Cache-Control", "no-cache");
			response.setCharacterEncoding("UTF-8");
			PrintWriter pw = null;
			List<Object[]> art_l = new ArrayList();
			int total = 0;
			
			if("y_cls".equals(artType)){
				art_l = yArticleAccessor.getArtByClsAll(rootCls, first, Constant.PAGE_MAX_MANAGER);
				total = yArticleAccessor.getCountByCls(rootCls);
			}else if("s_cls".equals(artType)){
				art_l = sArticleAccessor.getArtByClsAll(rootCls, first, Constant.PAGE_MAX_MANAGER);
				total = sArticleAccessor.getCountByCls(rootCls);
			}else if("z_cls".equals(artType)){
				art_l = zArticleAccessor.getArtByClsAll(rootCls, first, Constant.PAGE_MAX_MANAGER);
				total = zArticleAccessor.getCountByCls(rootCls);
			}else if("x_cls".equals(artType)){
				art_l = xArticleAccessor.getArtByClsAll(rootCls, first, Constant.PAGE_MAX_MANAGER);
				total = xArticleAccessor.getCountByCls(rootCls);
			}
			System.out.println(artType+"-->"+first+"-->"+art_l.size()+"---------->"+total+"-->"+rootCls);
			JSONArray reJA = new JSONArray();
			for(Object[] _t : art_l){
				JSONArray aiJa = new JSONArray();
				aiJa.put("<input type=checkBox name=\"cbId\" value=\""+_t[0]+"\" >");
				aiJa.put(_t[1]);
				aiJa.put(_t[12]);
				aiJa.put(_t[23]);
				aiJa.put(_t[15]);
				aiJa.put(_t[14]);
				aiJa.put(_t[13]);
				aiJa.put(_t[18]);
				reJA.put(aiJa);
			}
			int cPage = total == 0?0: total/Constant.PAGE_MAX_MANAGER+1;
			try{
				pw = response.getWriter();
				JSONObject jo = new JSONObject();
				jo.put("result", "1");
				jo.put("msg", reJA);
				jo.put("page", ""+cPage);
				System.out.println(jo.toString());
				pw.write(jo.toString());
			}catch(Exception e){
				log.error("获取文章出现异常.",e);
				pw.write("{\"result\":\"0\",\"msg\":\""+e.getMessage()+"\"}");
			}
			return null;
		}
		return Constant.ADMIN_LOGIN;
	}

	public String getRootCls() {
		return rootCls;
	}

	public void setRootCls(String rootCls) {
		this.rootCls = rootCls;
	}
}
